﻿using Developer.Core.FrameWork.MiddlePlatformService.Options;
using Developer.Core.FrameWork.MiddlePlatformService.Service;
using Developer.Core.FrameWork.MiddlePlatformService.Service.Impl;
using Developer.Core.FrameWork.Pollys.Extensions;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Developer.Core.FrameWork.MiddlePlatformService.Extensions
{
    /// <summary>
    /// 中台服务扩展类
    /// </summary>
    public static class MiddlePlatformServiceCollectionExtensions
    {
        public static IServiceCollection AddMiddlePlatformService(this IServiceCollection services)
        {
            AddMiddlePlatformService(services,options=> { });

            return services;
        }

        public static IServiceCollection AddMiddlePlatformService(this IServiceCollection services,Action<MiddlePlatformOptions> action)
        {
            // 0、中台配置选项
            MiddlePlatformOptions middlePlatformOptions = new MiddlePlatformOptions();
            action(middlePlatformOptions);

            // 1、中台配置选项注入IOC容器
            services.Configure<MiddlePlatformOptions>(action);

            // 2、添加httpclient
            services.AddPollyHttpClient(middlePlatformOptions.HttpClientName, middlePlatformOptions.pollyHttpClientOptions);

            // 3、注册中台
            services.AddSingleton<IMiddlePlatformService, HttpMiddlePlatformService>();

            return services;
        }
    }
}
